/*
 * fake_shell.c
 *
 *  Created on: 2016-11-10
 *      Author: m
 */

#include "fake_shell.h"
#include "string.h"
#include "keyboard.h"
#include "global.h"
#include "proc.h"
#include "hd.h"

static char s_cmd[32] = {0};
static u8 s_write_cmd_pos = 0;

static void print_proc_info() {
    int i = 0;

    printf("name     tick state prio recv_from send_to next_sending q_sending\n");
    for (i = 0; i < NR_TASKS; ++i) {
        PROCESS* p = &g_proc_table[i];
        printf("%s 0x%X 0x%x   ", p->name, p->ticks, p->state);
        printf("0x%x 0x%x       0x%x      ", p->priority, p->ipc.recv_from, p->ipc.send_to);

        if(p->ipc.next_sending) {
            printf("0x%x     ", p->ipc.next_sending->pid);
        } else {
            printf("0x%x     ", TASK_NONE);
        }

        if(p->ipc.q_sending) {
            printf("0x%x\n", p->ipc.q_sending->pid);
        } else {
            printf("0x%x\n", TASK_NONE);
        }
    }
}

static void fdisk_l() {
    MESSAGE msg = {0};
    msg.type = MT_FDISK_LIST;
    post(FN_BOTH, TASK_IDX_HD_DRV, &msg);
}

static void parse_cmd(char* pcmd) {
    if (0 == strcmp("ps", pcmd)) {
        print_proc_info();
    } else if (0 == strcmp("hi", pcmd)) {
        MESSAGE msg = {0};
        msg.type = MT_DEV_OPEN;
        post(FN_BOTH, TASK_IDX_HD_DRV, &msg);
    } else if(0 == strcmp("fdl", pcmd)) {
        fdisk_l();
    } else {
        printf("error cmd! some help.");
    }
}

void on_usr_kbhit_c(u8 c) {
    if(s_write_cmd_pos < sizeof(s_cmd)) {
        s_cmd[s_write_cmd_pos] = c;
        s_write_cmd_pos++;
    } else {
        //output warning
    }
}

void on_usr_kbhit_specail(u8 vkey) {
    if(KEY_ENTER == vkey || KEY_KPENTER == vkey) {
        parse_cmd(s_cmd);
        memset(s_cmd, 0, sizeof(s_cmd));
        s_write_cmd_pos = 0;
    }
}


